package com.haisen.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haisen.core.utils.Result;
import com.haisen.exception.BizException;
import com.haisen.service.HiveJdbcTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.*;

@Service
@Slf4j
public class HiveJdbcTemplateServiceImpl implements HiveJdbcTemplateService{

    @Autowired
    @Qualifier("hiveDruidTemplate")
    private JdbcTemplate hiveDruidTemplate;

    @Autowired
    @Qualifier("hiveDruidDataSource")
    DataSource druidDataSource;

    public String createTable(String sqlCommand){
        String result = "Create table successfully...";
        try {
            hiveDruidTemplate.execute(sqlCommand);
        }catch (DataAccessException dae) {
            result = "Create table encounter an error: " + dae.getMessage();
        }
        return result;
    }

    @Override
    public String dropTable(String sqlCommand) {
        String result = "drop table successfully...";
        try {
            hiveDruidTemplate.execute(sqlCommand);
        }catch (DataAccessException dae) {
            result = "drop table encounter an error: " + dae.getMessage();
        }
        return result;
    }

    @Override
    public String updateTable(String sqlCommand) {
        String result = "update table successfully...";
        try {
            hiveDruidTemplate.execute(sqlCommand);
        }catch (DataAccessException dae) {
            result = "update table encounter an error: " + dae.getMessage();
        }
        return result;
    }
    public String insertTable(String sqlCommand){
        String result = "Insert table successfully...";
        try {
            hiveDruidTemplate.execute(sqlCommand);
        }catch (DataAccessException dae) {
            result = "Insert table encounter an error: " + dae.getMessage();
        }
        return result;
    }

    @Override
    public JSONArray readTable(String sqlCommand) {
        JSONArray arr = new JSONArray();
        Connection connection = null;
        Statement statement = null ;
        ResultSetMetaData rsma = null;
        int size = 0;
        try {
            connection = druidDataSource.getConnection();
            statement = connection.createStatement();
            ResultSet resultSet =  statement.executeQuery(sqlCommand);
            rsma = resultSet.getMetaData();
            size = rsma.getColumnCount();
            while (resultSet.next()) {
                JSONObject obj = new JSONObject();
                for (int i = 1; i <= size; i++) {
                    String s = resultSet.getString(rsma.getColumnName(i)) == null?"":resultSet.getString(rsma.getColumnName(i));
                    if(s==null||"".equals(s)){
                        s="";
                    }
                    obj.put(rsma.getColumnName(i), s);
                }
                arr.add(obj);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return arr;
    }

    public JSONArray descTable(String tblName){
        String sqlCommand = "desc "+tblName;
        JSONArray arr = new JSONArray();
        Connection connection = null;
        Statement statement = null ;
        try {
            connection = druidDataSource.getConnection();
            statement = connection.createStatement();
            ResultSet resultSet =  statement.executeQuery(sqlCommand);
            while (resultSet.next()) {
              JSONObject obj = new JSONObject();
              obj.put("col_name",resultSet.getString("col_name"));
              obj.put("data_type",resultSet.getString("data_type"));
              obj.put("comment",resultSet.getString("comment"));
//                log.info("{},{},{}",resultSet.getString("col_name"),resultSet.getString("data_type"),resultSet.getString("comment"));
                arr.add(obj);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return arr;
    }



}
